<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\captcha;
use think\Session;
class Index extends Base
{
    public function index()
    {
        $this->isLogin();
        $staff =  Session::get('STAFF');
        $staffImg = $staff['staff_photo'];
        $this->assign("staff",$staff);
        $this->assign("staffImg",$staffImg);
        return $this->fetch();
    }
    
    public function login(){
        return $this->view->fetch();
    }
    
    
    public function toLogin(){
        $res= Request::instance()->post();
        $login_name=$res['login_name'];
        $login_pwd=$res['login_pwd'];
        $code=$res['code'];

        if(!$this->check_verify($code)){
            echo 2;
            exit;
            };

        $result=Db::table('staff')->where('login_name',$login_name)->find();

        if($result){
            if(md5($login_pwd.$result['safety_code'])==$result['login_pwd']){
                Session::set('STAFF',$result);
                echo 0;
            }else{
                echo 1;
            }
        }else{
            echo 1;
        }
    }
    /*
     * 登录时验证码
     *
     * */
    public function verify(){
        $config =    [
            // 验证码字体大小
            'fontSize'    =>    30,
            // 验证码位数
            'length'      =>    4,
            // 关闭验证码杂点
            'useNoise'    =>    false,
        ];
        $captcha = new captcha\Captcha($config);
        return $captcha->entry();
    }
    /*
     * 检验验证码
     *
     * */
    public function check_verify($code, $id = ''){

        $captcha = new captcha\Captcha();

        return $captcha->check($code, $id);
    }
    /*
     * 退出
     * */
    public function login_out(){
        Session::destroy();
        echo json_encode(0);
    }
    /*
     * 欢迎信息界面
     * */ 
    public function welcome(){
        $res= Session::get('STAFF');
        $this->assign("login_name",$res['login_name']);
        $this->view->assign("last_time",$res['last_time']);
        $this->view->assign("login_ip",$res['login_ip']);
        $this->view->assign("staff_photo",$res['staff_photo']);
        return $this->view->fetch();

    }







}

